var ctx = window.daijia.ctx;

/**
 * 验证表单
 * @returns
 */
function valid() {
    var serviceType = $('#serviceType').val();
    var contType = $('#contType').val();
    var title = $('#title').val();
    var author = $('#author').val();
    var created = $('#created').val();
    var link = $('#link').val();
    var picture = $('#picture').val();
    var content = $('#content').val();
    if (document.getElementById("companyId")) {
        var companyId = $("#companyId").val();
        if (companyId == null || companyId == '') {
            $.scojs_message('请您选择要添加广告的公司', $.scojs_message.TYPE_ERROR);
            return false;
        }
    }
    if (serviceType == null || serviceType == '') {
        $.scojs_message('请您选择要添加广告的服务类型', $.scojs_message.TYPE_ERROR);
        return false;
    }
    if (contType == null || contType == '') {
        $.scojs_message('请您选择要添加广告的终端类型', $.scojs_message.TYPE_ERROR);
        return false;
    }
    if (title == null || title == '') {
        $.scojs_message('请您为您的广告添加标题', $.scojs_message.TYPE_ERROR);
        return false;
    }
    if (author == null || author == '') {
        $.scojs_message('请您为您的广告指定作者', $.scojs_message.TYPE_ERROR);
        return false;
    }
    if (created == null || created == '') {
        $.scojs_message('请您为您的广告添加创建时间', $.scojs_message.TYPE_ERROR);
        return false;
    }
    /*if (link == null || link == '') {
        $.scojs_message('请您为您的广告添加链接', $.scojs_message.TYPE_ERROR);
        return false;
    }*/
    // if (picture == null || picture == '') {
    // 	$.scojs_message('请您为您的广告添加广告图片', $.scojs_message.TYPE_ERROR);
    // 	return false;
    // }
    /*if (content == null || content == '') {
        $.scojs_message('请您为您的广告编写内容', $.scojs_message.TYPE_ERROR);
        return false;
    }*/
    if ((content == null || content == '') && (link == null || link == '')) {
        $.scojs_message('请您为您的广告添加链接或者编写内容，必须有且只能有一个，[内容/链接二选一]', $.scojs_message.TYPE_ERROR);
        return false;
    }
    if (content != null && content != '' && link != null && link != '') {
        $.scojs_message('广告链接和广告内容只能选填一个', $.scojs_message.TYPE_ERROR);
        return false;
    }
    return true;
}

/**
 * 提交创建广告
 * @returns
 */
function submit() {
    var v = valid();
    if (v) {
        $("#submit_button").attr("disabled", "disabled");
        var formData = new FormData($("#submit_form")[0]);
        $.ajax({
            type: "POST",
            url: ctx + '/news/createNews',
            // 告诉jQuery不要去处理发送的数据
            processData : false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            data: formData,
            success: function(result){
                if(result.success){
                    $.scojs_message('添加成功', $.scojs_message.TYPE_OK);
                    setTimeout(function(){window.location="/news/list";},1200);
                }
                else {
                    $("#submit_button").attr("disabled",false);
                    $("#CSRFToken").val(data.object);
                    $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
                    $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
                }
            }
        });
    }
}

$(function () {

    $("#created").datetimepicker({
        format: 'yyyy-mm-dd HH:ii:ss',
        language: 'zh-CN',
        minView: 'hour',
        pickDate: true,
        pickTime: false,
        inputMask: false,
        autoclose: true,
        endDate: new Date()
    });

    // 富文本编辑器
    KindEditor.ready(function (K) {
        K.create('#content', {
            filterMode: false,
            uploadJson: ctx + '/common/kindeditor/fileupload',
            allowUpload: true,
            fileManagerJson: ctx + '/common/kindeditor/filemanager',
            allowFileManager: true,
            height: 400,
            width: '100%',
            resizeType: 0,
            afterCreate: function () {
                var self = this;
                K.ctrl(document, 13, function () {
                    self.sync();
                });
                K.ctrl(self.edit.doc, 13, function () {
                    self.sync();
                });
            },
            afterBlur: function () {
                this.sync();
            }
        });
    });

    // 发布按钮添加监听事件
    $('#submit_button').click(function () {
        submit();
    })

    // //广告导图上传
    // $('#picture_upload_dialog').click(function () {
    //     $("#picture_file").click();
    // });

    // // 侦测图片选择
    // $(document).on('change', '#picture_file', function () {
    //     $.ajaxFileUpload({
    //         url: ctx + '/common/uploadTemp',
    //         secureuri: false,
    //         fileElementId: 'picture_file',
    //         dataType: 'json',
    //         data: {
    //             'type': 'news'
    //         },
    //         success: function (result) {
    //             if (result.success) {
    //                 $.scojs_message("上传成功", $.scojs_message.TYPE_OK);
    //                 $("#picture_img").attr("src", ctx + "/upload/news/" + result.url);
    //                 $("#picture").val("/upload/news/" + result.url);
    //             } else {
    //                 $.scojs_message(result.msg, $.scojs_message.TYPE_ERROR);
    //             }
    //         }
    //     });
    // });
})



